package cn.safekeeper.simple.web;

import cn.safekeeper.common.annotations.SafeKeeperBasicAuth;
import cn.safekeeper.common.annotations.SafeKeeperHasPermission;
import cn.safekeeper.common.annotations.SafeKeeperHasRole;
import cn.safekeeper.common.constant.SafeKeeperConstant;
import cn.safekeeper.core.SafeKeeper;
import cn.safekeeper.core.manager.SafeKeeperManager;
import cn.safekeeper.plugin.web.Result;
import cn.safekeeper.simple.web.vo.UserVO;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * SafeKeeper默认会放行/login访问
 */
@RestController
public class SimpleController {

    private static final String name="admin";
    private static final String password="admin";

    @RequestMapping("/sayHello")
    public String sayHello(){
        return "Dear developers, welcome to the SafeKeeper world!";
    }

    @RequestMapping("/safeKeeper")
    @SafeKeeperHasRole(roles = {"user"})
    public String safeKeeper(){
        return "Dear developers, You should want to log in after access!";
    }

    @RequestMapping("/clearData")
    @SafeKeeperHasPermission(permissions = {"user:usermanager:add"})
    public String clearData(@RequestBody UserVO user){
        SafeKeeperManager.getSafeKeeperTokenRealm().clearRoleOrPermissionCacheData(user.getId(),SafeKeeperManager.getConfig().getDefaultLoginType(),0);
        return "clear role cache data successfully!";
    }
    @RequestMapping("/xxx")
    @SafeKeeperBasicAuth
    public String xxx(@RequestBody UserVO user){
//        SafeKeeperManager.getSafeKeeperTokenRealm().clearRoleOrPermissionCacheData(user.getId(),SafeKeeperManager.getConfig().getDefaultLoginType(),1);
        return "clear permission cache data successfully!";
    }

    @RequestMapping("/clearPer")
    @SafeKeeperHasPermission(permissions = {"order:usermanager:save"})
    public String clearPer(@RequestBody UserVO user){
        SafeKeeper.safeDefault().clearRoleOrPermissionCacheData(user.getId(),1);
        return "clear permission cache data successfully!";
    }

    @RequestMapping("/delUser")
    @SafeKeeperHasRole(roles = {"admin"})
    public String delUser(){
        return "Dear developers, You should want to log in after access!";
    }

    @RequestMapping("/login")
    public Result login(@RequestBody UserVO user, HttpServletRequest request) {
        if (!StringUtils.isEmpty(user)) {
            //默认登录登录的逻辑
            if (user.getUsername().equals(name) && user.getPassword().equals(password)) {
                //如果没有loginType就用默认的即可
                if (request.getHeader(SafeKeeperConstant.LOGIN_TYPE) == null) {
                    if (user.getDevice() != null) {
                        SafeKeeper.safeDefault().login(user.getId(), user.getDevice());
                    } else {
                        SafeKeeper.safeDefault().login(user.getId());
                    }
                    return Result.succeed(SafeKeeper.safeDefault().getSafeKeeperInfo(), "登录成功");
                } else {
                    if (user.getDevice() != null) {
                        SafeKeeper.safeLogic(request.getHeader(SafeKeeperConstant.LOGIN_TYPE)).login(user.getId(), user.getDevice());
                    } else {
                        SafeKeeper.safeLogic(request.getHeader(SafeKeeperConstant.LOGIN_TYPE)).login(user.getId());
                    }
                }
            }
        }
        return Result.failedWith(null, 1001, "用户名密码错误");
    }
}
